<?php
/**
 * Prado Portal
 *
 * @author Steen Rabol <steen.rabol@gmail.com>
 * @link http://www.pradoportal.dk/
 * @copyright Copyright &copy; 2006,2007,2008 Steen Rabol
 * @license http://www.pradoportal.dk
 * @version $Id: TADxMenu.php 289 2008-11-14 05:34:40Z steen.rabol@gmail.com $
 * @package Pradoportal.Portlets.AdminPortlet.Admin
 *
 */

Prado::using('System.Web.UI.WebControls.TWebControl');
Prado::using('System.Web.UI.WebControls.THyperLink');

class TADxMenu extends TWebControl
{
	const PUBLIC_PATH = '';
	const STYLE_FILE = 'TADxMenu.css';

	private $_tagName = 'ul';
	private	$_cssclass	= 'tadxmenu';

	public function getCssClass()
	{
		return $this->_cssclass;
	}
	public function setCssClass($value)
	{
		$this->_cssclass	= $value;
	}

	public function publishWorkerFiles()
	{
		$url = Prado::getApplication()->getAssetManager()->publishFilePath(dirname(__FILE__).'/'.self::PUBLIC_PATH);
		$this->getPage()->getClientScript()->registerStyleSheetFile($url, $url.'/'.self::STYLE_FILE);
		return $url;
	}

	public function onPreRender($param)
	{
		$this->publishWorkerFiles();
	}

	public function render($writer)
	{
		//$writer->addAttribute('class', 'tadxmenu');
		$writer->addAttribute('class', $this->CssClass);
		$writer->renderBeginTag('ul');
		parent::renderChildren($writer);
		$writer->renderEndTag();
	}

	protected function getTagName()
	{
		return $this->_tagName;
	}

	public function addParsedObject($menuItem)
	{
		if($menuItem instanceof TADxMenuItem)
			$this->Controls[] = $menuItem;
		elseif(!is_string($menuItem))
			throw new TInvalidDataTypeException('Children of TADxMenu must be instances of TADxMenuItem ('.get_class($menuItem).' given)');
	}

	public function renderBeginTag($writer)
	{
		parent::renderBeginTag($writer);

		$writer->write("\n");
		$writer->renderBeginTag($this->_tagName);
		$writer->write("\n");
	}

	public function renderEndTag($writer)
	{
		$writer->write("\n");
		$writer->renderEndTag($this->_tagName);
		$writer->write("\n");
		parent::renderEndTag($writer);
	}
}

class TADxMenuItem extends TWebControl implements INamingContainer
{
	private $_childControl;
	private $_childControlType = 'InvalidWebControl';
	private $_hasControlsClass = 'HasControls';
	private $_tagName = 'ul';

	public function addParsedObject($object)
	{
		if($object instanceof TADxMenuItem)
		{
			$this->Controls->add($object);
		}
		elseif(!is_string($object))
		{
			throw new TInvalidDataTypeException('Children of TADxMenu must be instances of TADxMenuItem !!('.get_class($menuItem).' given)');
		}
	}

	public function createChildControls()
	{
		$myClass = 'T'.$this->_childControlType;
		$this->_childControl = new $myClass;
		$this->_childControl->setID($this->_childControlType);
		$this->Controls->add($this->_childControl);
	}

	public function renderContents($writer)
	{
		$writer->write($this->_childControl->render($writer));
		$this->Controls->removeAt(0);

		if($this->HasControls)
		{
			$writer->write("\n");
			$writer->renderBeginTag($this->_tagName);
			$writer->write("\n");
		}

		parent::renderContents($writer);

		if($this->HasControls)
		{
			$writer->renderEndTag($this->_tagName);
			$writer->write("\n");
		}
	}

	public function renderEndTag($writer)
	{
		parent::renderEndTag($writer);
		$writer->write("\n");
	}

	public function getHasControlsClass()
	{
		return $this->hasControlsClass;
	}

	public function setHasControlsClass($value)
	{
		$this->_hasControlsClass = TPropertyValue::ensureString($value);
	}

	public function getLinkButton()
	{
		$this->_childControlType = 'LinkButton';
		$this->ensureChildControls();
		return $this->_childControl;
	}

	public function getHyperLink()
	{
		$this->_childControlType = 'HyperLink';
		$this->ensureChildControls();
		return $this->_childControl;
	}

	protected function getTagName()
	{
		return 'li';
	}
}

class TADxMenuSeparator extends TADxMenuItem
{
	public function renderContents($writer)
	{
		$writer->write('<hr />');
	}

	public function createChildControls()
	{
		// no possible child controls = do nothing
	}

	public function getHyperLink()
	{
		throw new TInvalidOperationException('TADxMenuSeparator does not have a HyperLink property');
	}

	public function addParsedObject($object)
	{
		throw new TInvalidOperationException('TADxMenuSeparator cant have children');
	}
}
?>